﻿using System;
using System.Data.SqlClient;
using Pantry.Commons;
using Pantry.DataAccess;
using System.Data;
using Pantry.Entity;

namespace Pantry.Business
{
    public sealed class OrganizationBus 
    {
        private OrganizationDao _orDao;

        public OrganizationBus()
        {
            _orDao = new OrganizationDao();
        }

        /// <summary>
        /// Selects all Organization.
        /// </summary>
        /// <returns></returns>
        public DataTable SelectAllOrganization()
        {
            _orDao = new OrganizationDao();
            return _orDao.SelectAllOrganization();
        }

        /// <summary>
        /// Inserts the Organization.
        /// </summary>
        /// <param name="organizations">The Organizations.</param>
        /// <returns></returns>
        public bool InsertOrganizations(Organization organizations)
        {
            _orDao = new OrganizationDao();
            return _orDao.Insert(organizations);
        }
        /// <summary>
        /// Edits the Organizations.
        /// </summary>
        /// <param name="organizations">The Organizations.</param>
        /// <returns></returns>
        public bool EditOrganizations(Organization organizations)
        {
            _orDao = new OrganizationDao();
            return _orDao.Edit(organizations);
        }
        /// <summary>
        /// Checks the Organization name exists.
        /// </summary>
        /// <param name="organizationName">Name of the Organization.</param>
        /// <returns></returns>
        public bool CheckOrganizationNameExists(string organizationName)
        {
            _orDao = new OrganizationDao();
            return _orDao.CheckOrganizationNameExists(organizationName);

        }
        public bool MarkActivated(bool isActive, int organizationId)
        {

            _orDao = new OrganizationDao();
            return _orDao.MarkActivated(isActive, organizationId);
        }

        /// <summary>
        /// Selects the supporting material by id.
        /// </summary>
        /// <param name="id">The id.</param>
        public Organization ReturnOrganizationById(int id)
        {
            _orDao = new OrganizationDao();
            //_orDao.SelectSupportingMaterialById(url, des, type, addedBy, addedDate, id);
            return _orDao.ReturnOrganizationById(id);
        }
        /// <summary>
        /// Lists the organization role.
        /// </summary>
        /// <returns></returns>
        public DataTable ListOrganizationRole(int serviceId)
        {
            try
            {
                _orDao = new OrganizationDao();
                return _orDao.ListOrganizationRole(serviceId);
            }
            catch (SqlException sqlException)
            {
                ExceptionHandler.WriteErrorLog(sqlException);
                return null;
            }
            catch(ArgumentNullException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return null;
            }
        }
        /// <summary>
        /// Deletes all organization role.
        /// </summary>
        /// <param name="organizationId">The organization id.</param>
        /// <returns></returns>
        public bool DeleteAllOrganizationRole(int organizationId)
        {
            try
            {
                var result = _orDao.DeleteOrganizationRole(organizationId);
                return (result != 0);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return false;
            }
        }
        /// <summary>
        /// Updates the organization role.
        /// </summary>
        /// <param name="organizationId">The organization id.</param>
        /// <param name="roleId">The role id.</param>
        /// <returns></returns>
        public bool UpdateOrganizationRole(int organizationId, int roleId)
        {
            try
            {
                _orDao = new OrganizationDao();
                var result = _orDao.UpdateOrganizationRole(organizationId, roleId);
                return (result != 0);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return false;
            }
        }
        /// <summary>
        /// Returns the Organization by id.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public DataTable SelectOrganizationId(string name)
        {
            try
            {
                _orDao = new OrganizationDao();
                var result = _orDao.SelectOrganizationId(name);
                return result;
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return null;
            } 
        }
       public Organization RetrieveOrganizationIdByDepartment(int departmentId)
       {
           try
           {
               _orDao = new OrganizationDao();
               var result = _orDao.RetrieveOrganizationIdByDepartment(departmentId);
               return result;
           }
           catch (SqlException exception)
           {
               ExceptionHandler.WriteErrorLog(exception);
               return null;
           }   
       }
    }
}
